#include "../Field/Field2D.h"

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;



template<class Type>
Field2D<Type>::Field2D() : Field<Type>()
{
    this->data_=NULL;
}

template<class Type>
Field2D<Type>::Field2D(vector<int> dims) : Field<Type>(dims)
{
    this->data_=NULL;
    allocate_dims(dims);
}

template<class Type>
Field2D<Type>::Field2D(vector<int> dims, string name) : Field<Type>(dims, name)
{
    this->data_=NULL;
    allocate_dims(dims);
}

template<class Type>
Field2D<Type>::~Field2D()
{

    if (this->data_!=NULL) {
        delete [] this->data_;
        delete [] data_2D;
    }
}

template<class Type>
void Field2D<Type>::allocate_dims(std::vector<int> dims )
{
    this->dims_=dims;
    if (this->dims_.size()!=2) log_error<<"Alloc d dim must be 2 : " << dims.size();
    if (this->data_!=NULL) delete [] this->data_;
	
    this->data_ = new Type[this->dims_[0] * this->dims_[1]];
	
    data_2D = new Type*[this->dims_[0]];
    for (int i = 0; i < this->dims_[0]; i++)
    {
        data_2D[i] = this->data_ + i * this->dims_[1];
        for (int j = 0; j < this->dims_[1]; j++)
        {
            data_2D[i][j] = 0.0;
        }
    }
	
    this->dims_global_ = this->dims_[0] * this->dims_[1];
	
}

template<class Type>
Field2D<Type>& Field2D<Type>::operator=(const Field2D<Type>& field2D)
{
    if(this->dims_[0] != field2D.dims_[0] || this->dims_[1] != field2D.dims_[1])
    {
        log_error<<"dims not equal";
    }
    for(int i = 0; i < this->dims_global_; i++)
    {
        this->data_[i] = field2D.data_[i];
    }

    return *this;
}


template class Field2D<double>;
template class Field2D<int>;
